﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Threading;
using System.Drawing;
using System.IO;
using System.IO.IsolatedStorage;

namespace WPF101_ApplicationManagement
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        bool isApplicationActived = false;
        string filename = "app.txt";

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            try
            {
                // Restore application-scope property from isolated storage
                IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForDomain();
                using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(filename, FileMode.Open, storage))
                using (StreamReader reader = new StreamReader(stream))
                {
                    // Restore each application-scope property individually
                    while (!reader.EndOfStream)
                    {
                        string[] keyValue = reader.ReadLine().Split(new char[] {','});
                        this.Properties[keyValue[0]] = keyValue[1];
                    }
                }
            }
            catch (Exception ex)
            {


            }

            // Application is running
            // Process command line args
            bool startMinimized = false;
            for (int i = 0; i != e.Args.Length; ++i)
            {
                if (e.Args[i] == "/StartMinimized")
                {
                    startMinimized = true;
                }
            }

            Person p = new Person();
            p.FirstName = "FirstName";
            p.Lastname = "LastName";

            // Application scope properties
            Application.Current.Properties["MyAppProperty"] = "myAppProerty";
            Application.Current.Properties["MyPersonClass"] = p;

            // Application scope resource
            Application.Current.Resources["ApplicationScopeResource"] = Brushes.White;
            

            // Create main application window, starting minimized if specified
            MainWnd mainWindow = new MainWnd();
            if (startMinimized)
            {
                mainWindow.WindowState = WindowState.Minimized;
            }
            mainWindow.Show();

        }

        private void Application_Exit(object sender, ExitEventArgs e)
        {
            // Get the application exit code
            int exitCode = e.ApplicationExitCode;

            // Get the current appliction object
            Application app = Application.Current;

            // Persist application-scope property to isolated storage
            IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForDomain();
            using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(filename, FileMode.Create, storage))
            using (StreamWriter writer = new StreamWriter(stream))
            {
                // Persist each application-scope property individually
                foreach (string key in this.Properties.Keys)
                {
                    writer.WriteLine("{0},{1}", key, this.Properties[key]);
                }
            }

        }

        private void Application_Activated(object sender, EventArgs e)
        {
            isApplicationActived = true;
        }

        private void Application_Deactivated(object sender, EventArgs e)
        {
            isApplicationActived = false;
        }

        private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            // Prevent default unhandled exception processing
            e.Handled = true;
        }
    }

    public class Person
    {
        public string FirstName { get; set; }
        public string Lastname { get; set; }
    }
}
